/**
 * @author YangTao
 * @date 2025/4/18
 */
package com.tian.source.controller;

import com.tian.core.beans.ResultBean;
import com.tian.core.page.Page;
import com.tian.source.model.Job;
import com.tian.source.model.vo.JobVo;
import com.tian.source.service.DepartmentService;
import com.tian.source.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 添加职位
     * @param job
     * @return
     */
    @PostMapping("/addJob")
    public ResultBean<String> addJob(Job job){
        jobService.add(job);
        return new ResultBean<>("添加成功");
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param departmentId
     * @param name
     * @return
     */
    @GetMapping("/getAllByPage")
    public ResultBean<Page<List<JobVo>>> getAllByPage(Integer pageNum, Integer pageSize, Integer departmentId, String name){
        Page page = new Page(pageNum,pageSize);
        return new ResultBean<>(jobService.findAllByPage(page,departmentId,name));
    }

    /**
     * 根据Id查询
     * @param jobId
     * @return
     */
    @GetMapping("/getInfoById")
    public ResultBean<Map<String,Object>> getInfoById(Integer jobId){
        //获取全部部门
        Map<String,Object> map = new HashMap<>();
        map.put("department",departmentService.getAll());
        map.put("job",jobService.findById(jobId));
        return new ResultBean<>(map);
    }

    /**
     * 修改
     * @param job
     * @return
     */
    @PostMapping("/updateJob")
    public ResultBean update(Job job){
        jobService.modifyById(job);
        return new ResultBean();
    }

    @GetMapping("/getJobByDeptId")
    public ResultBean<List<Job>> getJobByDeptId(Integer deptId){
        return new ResultBean<>(jobService.getJobByDeptId(deptId));
    }

    @GetMapping("/getJobOfAll")
    public ResultBean<List<Job>> getJobOfAll(){
        return new ResultBean<>(jobService.getAll());
    }
}
